<?php
namespace App\Logics;

use Dcat\Admin\Admin;
use Illuminate\Support\Facades\DB;

class Logic {

    public $admin_user;

    public function __construct($admin_user = null)
    {
        $this->admin_user = $admin_user;
    }

    public function getAdminUser()
    {
        return Admin::guard()->check() ? Admin::user() : null;
    }

    /**
     * Create a form instance.
     *
     * @param  mixed  ...$params
     * @return $this
     */
    public static function make(...$params)
    {
        return new static(...$params);
    }


    public static function getBaseQuery($query, $admin_user, $is_humman_table = true){
        $is_super_manger = $admin_user->isSuperManger();
        if (!$is_super_manger) {
            $query->where('dwdm', $admin_user->dwdm);
        } elseif($is_humman_table){
            $query->where('is_main', 1);
        }

        return $query;
    }

    public static function getBaseDBQuery($table, $sql, $admin_user, $is_humman_table = true){
        $query = DB::table($table);

        $is_super_manger = $admin_user->isSuperManger();
        if (!$is_super_manger) {
            $query->where('dwdm', $admin_user->dwdm);
        } elseif($is_humman_table){
            $query->where('is_main', 1);
        }
        $query->whereRaw($sql);
        return $query;
    }
}